package com.demo.virtual_thread;

import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;

public class ContinuationDemo {
    public static void main(String[] args) {
        ContinuationScope scope = new ContinuationScope("DemoScope");
        Continuation continuation = new Continuation(scope, () -> {
            System.out.println("1");
            Continuation.yield(scope);
            System.out.println("2");
            Continuation.yield(scope);
            System.out.println("3");
        });
        // 第一次运行
        continuation.run();
        System.out.println("do other things 1");
        // 第二次运行（从上次yield处继续）
        continuation.run();
        System.out.println("do other things 2");
        // 第三次运行（已终止）
        continuation.run(); // 抛出异常，因已终止
    }
}
